package com.oms.module.common.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.oms.module.admin.domain.entity.AdminEntity;
import com.oms.module.common.dao.AdminLoginMapper;
import com.oms.module.common.dao.UserMapper;
import com.oms.module.merchant.domain.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author zhanghailong QQ38592077
 * @Description TODO
 * @Date 2020/6/8 17:50
 */
@Service
public class UserLoginService implements UserDetailsService {
    @Autowired
    AdminLoginMapper adminLoginMapper;
    @Autowired
    UserMapper userMapper;

    /**
     *
     * @param
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String[] s = username.split("-");
        if ("a".equalsIgnoreCase(s[0])) {
            AdminEntity adminEntity = adminLoginMapper.selectOne(Wrappers.<AdminEntity>query().eq("username", username));
            if (adminEntity ==null){
                throw new UsernameNotFoundException("用户名不存在！");
            }
            return adminEntity;
        }else if ("b".equalsIgnoreCase(s[0])){
            AdminEntity adminEntity = adminLoginMapper.selectOne(Wrappers.<AdminEntity>query().eq("username", username));
            if (adminEntity ==null){
                throw new UsernameNotFoundException("用户名不存在！");
            }
            return adminEntity;
        }else{
            UserEntity userEntity = userMapper.selectOne(Wrappers.<UserEntity>query().eq("username",username));
            if (userEntity ==null){
                throw new UsernameNotFoundException("用户名不存在！");
            }
            return userEntity;
        }

    }
}
